package com.example.xunua_mymusic.utils;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.MediaStore;
import android.util.Log;


import com.example.xunua_mymusic.entitys.Music;

import java.util.ArrayList;

public class GetMusicList {
    public ArrayList<Music> getMusic(ContentResolver contentResolver){

        ArrayList<Music>mlist=new ArrayList<>();
        mlist.clear();
        Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        if (cursor.moveToFirst()) {
            for (int i = 0; i <cursor.getCount() ; i++) {
                long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));//获取ID
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));//获取歌曲名
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));//获取歌手名
                long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));//歌曲时长
                long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));//歌曲大小 5M
                String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));//专辑名称
                String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));//歌曲路径 int类型 音乐在手机中的位置
                int ismusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//判断是不是一首标准的音乐
                if (ismusic != 0&&duration>13000) {
                    Music music=new Music();
                    music.setId(id);
                    music.setTitle(title);
                    music.setArtist(artist);
                    music.setDuration(duration);
                    music.setSize(size);
                    music.setAlbum(album);
                    music.setUrl(url);
                    Log.i("TAG", "getMusic: "+title+"--"+artist);
                    mlist.add(music);
                }
                cursor.moveToNext();
            }
        }
        cursor.close();
        return mlist;
    }
}
